﻿using ConsumablesCabinet;
using Entity;
using IntelligentPharmacy.Entity;
using IntelligentPharmacy.publicclass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentPharmacy.httprequest
{
    class requestService
    {
        private requestDao redao = new requestDao();
        /// <summary>
        /// 注册柜子是否成功
        /// </summary>
        /// <param name="deviceMac"></param>
        /// <param name="deviceName"></param>
        /// <returns></returns>
        public bool InsertDeviceInfo(string deviceMac,string deviceName)
        {
            DeviceEntity device = new DeviceEntity();
            device.eqpMac = deviceMac;
            device.eqptName = deviceName;
            List<DeviceEntity> list = new List<DeviceEntity>();
            list.Add(device);
            information<SpdId> test= redao.InsertDeviceInfo(list);
            if (test.success)
            {
                Global.spdNameOrDreiceName = test.dataList == null ? null : test.dataList[0];
                ReadAppConfig.writeeqpMac(test.dataList[0].eqpMac);
                ReadAppConfig.writeeqptId(test.dataList[0].eqptId);
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 登录接口 empName  empPwd inputDate
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public bool register(string username,string password)
        {
            UserEntity user = new UserEntity();
            user.empNo = username;
            user.empPwd = password;
            user.eqpMac = DeviceInfo.GetMacAddress();
            user.inputDate ="";
   
            information<UserFace> users=redao.register(user);
            if (users.success && users.dataList != null && users.dataList[0].checkRet==1)
            {
                if (users.dataList.Count > 0)
                {
                    if(users.dataList.Count>0)
                        Global.User = users.dataList[0];
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取柜子所有的指纹信息和人脸信息
        /// </summary>
        /// <param name="kfid"></param>
        /// <returns></returns>
        public void getUserMessage(string kfid)
        {
            UserEntity user = new UserEntity();
            user.empNo = "";
            user.empPwd = "";
            user.eqpMac = DeviceInfo.GetMacAddress();
            user.inputDate = DateTime.Now.ToString("yyyy-MM-dd");
        
            information<UserFace> users = redao.register(user);
            if (users.success)
            {
                if (users.dataList.Count > 0)
                {
                    List<UserFace> listlist = new List<UserFace>();
                    List<UserFace> lists=users.dataList;
                    foreach(UserFace use in lists)
                    {
                        if (use.faceData.Length>40 || use.fpData.Length>50)
                        {
                            listlist.Add(use);
                        }
                    }
                    Global.listuserFace = listlist;
                }

            }
            else
            {

            }
        }
        /// <summary>
        /// 获取RFID所对应的耗材名称
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<MaterialnformationEntity> getRfidContent(List<TagEntity> list)
        {
            baseList<TagEntity> baselist = new baseList<TagEntity>();
            baselist.list = list;
            return redao.getInoutInfo(baselist).dataList;
        }
        /// <summary>
        ///保存记录出入库
        /// </summary>
        public bool outPutCabinet(List<TagEntity> list,string inputFlag)
        {
            List<OutPutEntity> lists = new List<OutPutEntity>();
           // string a= DateTime.Now.ToString("yyyyMMddHHmmssffff");
            foreach (TagEntity item in list)
            {
                OutPutEntity output = new OutPutEntity();
                output.rfid = item.rfid;
                output.inoutFlag = inputFlag;
                output.inputDate= DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
                Random rd = new Random();
                int i = rd.Next();
                output.inoutMstId = DateTime.Now.ToString("yyyyMMddHHmmssffff")+i + item.rfid;
                //ReadAppConfig.readerWarehouseName();
                output.eqpMac = ReadAppConfig.readereqpMac();
                output.empId = Global.User.empId;
                lists.Add(output);
            }

            foreach (OutPutEntity iut in lists)
            {
                Logger.WriteLog(iut.ToString());
            }
            baseList<OutPutEntity> baseList = new baseList<OutPutEntity>();
            baseList.list = lists;
            return redao.outPutCabinet(baseList);
        }
        /// <summary>
        /// 获取库存
        /// </summary>
        public List<TagEntity> getStockList()
        {
            Dictionary<string, string> obj = new Dictionary<string, string>();
            obj.Add("inStoreroomId", DeviceInfo.GetMacAddress());
            information<TagEntity> information= redao.getStorckList(obj);
            if (information.success==true)
            {
                return information.dataList;
            }
            return null;
        }
        public List<MaterialnformationEntity> getOutPutRecord(Querydto querydto)
        {
            information<MaterialnformationEntity>  infor=redao.getOutPutRecord(querydto);
            return infor.dataList;
        }
    }
}
